$(function () {
    var socket = null;
    var connectSocket = function () {
        if (typeof WebSocket == 'function') {
            socket = new WebSocket(window.skServer);
        } else if (typeof MozWebSocket == 'function') {
            socket = new MozWebSocket(window.skServer);
        }
        if (socket !== undefined && socket !== null) {
            socket.onopen = function () {
                socket.send(JSON.stringify({
                    cmd: 'set',
                    hotel: window.currentHotel
                }));
            };
            socket.onmessage = function (e) {
                var data = $.parseJSON(e.data);
                if (data.info) {
                    data = data.info.split(':');
                    switch (data[0]) {
                        case 'd':
                            setDirty($('.roomItem[data-room=' + data[1] + ']'), data[2], data[3]);
                            break;
                        default:
                            break;
                    }
                }
            };
            socket.onclose = function () {};
        }
    };
    var setDirty = function ($obj, status, timestamp) {
        status = parseInt(status);
        // 如果需要新设置的时间戳小于当前时间戳
        var originTime = $obj.data('time');
        if (originTime !== undefined && timestamp < originTime) {
            return false;
        }
        $obj.attr('data-status', status);
        $obj.data('status', status);
        $obj.data('time', timestamp);
//      $obj.find('span').attr('data-original-title', status == 1 ? lang('click.clean') : lang('click.dirty'));
    };
    // 获取脏房通知
    if ($('#doc').attr('data-room-status-auth')) {
        setTimeout(function () {
            connectSocket();
        }, 5000);
    } else {};
    var maintainContainer = $('#maintains-container');
    // 设置脏房干净房接口
    maintainContainer.on('click','.dirty-list',function(e){
        e.stopPropagation();
        if ($(this).attr('disabled') !== undefined) {
            return false;
        }        
        var $me = $(this);
        var status = $me.data('status') === 0 ? 'dirty' : 'clean';
        var param = {
                hotel: window.currentHotel,
                room: $me.data('room'),
                status: status
        };
        ajaxRequest('/Home/Room/setDirty', param, 'post', function (data) {
            if (data.data[0].value == window.obj.data('room')) {
                var timestamp = data.data[0].time;
                if (window.obj.data('status') === 0) {
                    setDirty(window.obj, 1, timestamp);
                    maintainContainer.find('.dirty-content').text(lang('click.clean'));
                } else {
                    setDirty(window.obj, 0, timestamp);
                    maintainContainer.find('.dirty-content').text(lang('click.dirty'));
                }
            }
        }, function (error) {
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        }, $me);    
    });  
    //维修房日期
    var today = $('#doc').data('today');
    var endtime = $('#repairModal').data('endtime');
    $('#repairStartTime').fdatepicker({
        format: 'yyyy-mm-dd',
        startDate: today,
        endDate: endtime,
    }); 
    
    $('#repairEndTime').fdatepicker({
        format: 'yyyy-mm-dd',
        startDate: today,
        endDate: endtime,
    }); 
    $('#repairModal').on('click','.close',function(){
        $('.datepicker').hide();
    });    
});
